<?php

/*
 * File application\modules\cli\controllers\IndexController.php
 * 
 */

/**
 * Index controller
 *
 * Index Controller is the default controller for cli
 *
 * This controller is used with system command.This controller is a cron job.
 * This is the default controller of this module.
 * @author 	   Gtl
 * @uses       Zend_Controller_Action
 * @category   Zend
 */
class IndexController extends Zend_Controller_Action {

    /**
     * Index Index Action
     *
     * The "index" action is the default action for all controllers
     * Assuming the default route and default router, this action is dispatched
     * via the following urls:
     *   /
     *   /index/
     *   /index/index
     *
     * @return void
     */
    public function indexAction() {
		
		/*
		echo APP_ROOT;
		$fp = fopen(APP_ROOT.'index.txt', 'a');
		fwrite($fp, "This is the symbol of cron job running\n");
		fclose($fp);
		echo "####";
		exit;
		*/
		
        //Load One Way API credentials from  registry
        $config = Zend_Registry::get('config');

        $ONE_WAY_API_USERNAME = $config->ONE_WAY_API_USERNAME;
        $ONE_WAY_API_PASSWORD = $config->ONE_WAY_API_PASSWORD;
        $ONE_WAY_STD_COUNTRY_CODE = $config->ONE_WAY_STD_COUNTRY_CODE;
        //Load Sms Log Model
        $smsLogModel = new Model_Smslog();

        //Load Admin Model and Admin
        $adminModel = new Model_Admin();
        $adminData = $adminModel->fetchEntryById("1");
	
        $projectModel = new Model_Project();
        $projectsData = $projectModel->fetchProjects();
		$customerArray = $projectModel->getCustomersArray();
		
        // Total days difference logic
        foreach ($projectsData as $_prj_key => $_project) {
            $projectid = $_project['prj_id'];
            $projectData = $projectModel->fetchEntryById($projectid);
            $project_referecneData = $projectData['PROJECT_REFERENCE'];

            $min_start_date = strtotime('2037-12-12'); //Set an MAximum date initially so can reduce and set minimum one of the date
            $max_end_date = 0; //Maximum End date is set initially a value of zero

            foreach ($project_referecneData as $_prj_ref) {
                $start_date = $_prj_ref['job_startdate'];
                $end_date = $_prj_ref['job_enddate'];
                if ($max_end_date < strtotime($end_date)) {
                    $max_end_date = strtotime($end_date);
                }
                if ($min_start_date > strtotime($start_date)) {
                    $min_start_date = strtotime($start_date);
                }
            }
            $projectsData[$_prj_key]['minimum_start_date'] = date('Y-m-d', $min_start_date); //Minimum Start Date set in the project Array
            $projectsData[$_prj_key]['maximum_end_date'] = date('Y-m-d', $max_end_date); //Maximum End Date set in the project Array
            $day_difference = GTL_Common::getDateDifference($projectsData[$_prj_key]['maximum_end_date'], $projectsData[$_prj_key]['minimum_start_date']);
            $projectsData[$_prj_key]['total_day_diff'] = $day_difference;
			$projectsData[$_prj_key]['customer_name'] = $customerArray[$_project["cus_id"]];

            //Calculate the total days remaining
            if (strtotime($projectsData[$_prj_key]['maximum_end_date']) > strtotime(date('Y-m-d'))) {
                $day_remaining = GTL_Common::getDateDifference($projectsData[$_prj_key]['maximum_end_date'], date('Y-m-d'));
            } else {
                $day_remaining = 0;
            }
            $projectsData[$_prj_key]['total_day_remaining'] = $day_remaining;
        }
		
        // Total days difference logic
        foreach ($projectsData as $_prj_key => $_project) {
		
            $project_name = $_project['prj_name'];
			
            $admin_mobile = $ONE_WAY_STD_COUNTRY_CODE . $adminData['adm_mobile'];
            $designer_name = $_project['dsg_name'];
            $designer_mobile = $ONE_WAY_STD_COUNTRY_CODE . $_project['dsg_mobile'];
            $total_day_remaining = $_project['total_day_remaining'];
			
			$msgDate = date('Y-m-d',strtotime("+5 day"));
			
            $designer_msg_text = $project_name . " for " . $_project['customer_name'] . "are going to be complete on " . $msgDate . ".\nFrom Kotzenproject.com";
			$admin_msg_text = $project_name . " for " . $_project['customer_name'] . "are going to be complete on " . $msgDate . ".\nFrom Kotzenproject.com";
					
            if ($total_day_remaining == '5') {
			
                //SMS API call to the Designer
                $returnDesignerCallResponse = GTL_Common::one_way_send_sms($ONE_WAY_API_USERNAME, $ONE_WAY_API_PASSWORD, $admin_mobile, $designer_mobile, $designer_msg_text);

                //Designer Log Array
                $designerLogArray['TYPE'] = 'DESIGNER';
                $designerLogArray['DESIGNER_ID'] = $_project['dsg_id'];
                $designerLogArray['DESIGNER_NAME'] = $designer_name;
                $designerLogArray['PROJECT_ID'] = $_project['prj_id'];
                $designerLogArray['PROJECT_NAME'] = $project_name;
                $designerLogArray['TOTAL_DAY_REMAINING'] = $total_day_remaining;
                $designerLogArray['MT_ID'] = $returnDesignerCallResponse['MT_ID'];
                $designerLogArray['RESULT'] = $returnDesignerCallResponse['RESULT'];
                $designerLogArray['SMS_FROM_MOBILE'] = $admin_mobile;
                $designerLogArray['SMS_TO_MOBILE'] = $designer_mobile;
                $designerLogArray['SMS_MESSAGE'] = $designer_msg_text;

                //Designer SMS Log 
                $smsDesignerLog = Array();
                $smsDesignerLog['sms_id'] = NULL;
                $smsDesignerLog['sms_log'] = serialize($designerLogArray);
                $smsDesignerLog['sms_createddate'] = date("Y-m-d H:i:s");
                $smsLogModel->save($smsDesignerLog, 'sms_id');

                //SMS API call to the Admin 
                $returnAdminCallResponse = GTL_Common::one_way_send_sms($ONE_WAY_API_USERNAME, $ONE_WAY_API_PASSWORD, $admin_mobile, $designer_mobile, $admin_msg_text);

                //Admin Log Array
                $AdminLogArray['TYPE'] = 'ADMIN';
                $AdminLogArray['ADMIN_ID'] = $adminData['adm_id'];
                $AdminLogArray['ADMIN_NAME'] = $adminData['adm_firstname'] . " " . $adminData['adm_lastname'];
                $AdminLogArray['PROJECT_ID'] = $_project['prj_id'];
                $AdminLogArray['PROJECT_NAME'] = $project_name;
                $AdminLogArray['TOTAL_DAY_REMAINING'] = $total_day_remaining;
                $AdminLogArray['MT_ID'] = $returnAdminCallResponse['MT_ID'];
                $AdminLogArray['RESULT'] = $returnAdminCallResponse['RESULT'];
                $AdminLogArray['SMS_FROM_MOBILE'] = $admin_mobile;
                $AdminLogArray['SMS_TO_MOBILE'] = $admin_mobile;
                $AdminLogArray['SMS_MESSAGE'] = $admin_msg_text;

                //Admin SMS Log 
                $smsAdminLog = Array();
                $smsAdminLog['sms_id'] = NULL;
                $smsAdminLog['sms_log'] = serialize($AdminLogArray);
                $smsAdminLog['sms_createddate'] = date("Y-m-d H:i:s");
                $smsLogModel->save($smsAdminLog, 'sms_id');
            }
        }
        $this->_helper->viewRenderer->setNoRender(true);
    }

}
